package org.carabiner.infinitest;

import org.junit.runner.JUnitCore;

public class JUnit4Runner implements TestRunner
{
  private TestRunnerEventSupport eventSupport;

  public JUnit4Runner()
  {
    eventSupport = new TestRunnerEventSupport(this);
  }
  
  public void addTestStatusListener(TestStatusListener listener)
  {
    eventSupport.addTestStatusListener(listener);
  }

  public void killTest()
  {
    // JUnit tests cannot be killed midstream
  }

  public void runTest(String testClass)
  {
    JUnitCore core = new JUnitCore();
    core.addListener(new TestEventAdapter(eventSupport));
    try
    {
      Class clazz = Class.forName(testClass);
      core.run(new Class[]{clazz});
    }
    catch (ClassNotFoundException e)
    {
      throw new IllegalArgumentException(testClass + " is not a class");
    }
  }
}
